<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo Yii::$app->name;?> 后台管理</title>
<?php $this->HeaderView(['template']);?>

<script>
function view(postid){
    var url = '<?php echo $this->createUrl('view')?>?postid='+postid;	
	art.dialog.open(url,{
		id:'view',
		width:480,
		lock:true,
		fixed:true,
		title:'帖子详情'
	});
}

</script>
</head>
<body class="view_index" id="view_index_Article">

<form class="view-index-form" method="post" action="<?php echo $this->createUrl(['mobo/models-form/batch', 'modelid'=> $modelid]);?>" enctype="multipart/form-data">
<input type="hidden" value="<?php echo $modelid?>" name="modelid" />
<table class="view-index-list" style="margin-top:0; margin-bottom:0; border-bottom:0" border="0" cellspacing="0" cellpadding="0" width="100%">
    <thead>
        <tr>
        <th align="center" width="20"><input type="checkbox" value="" name="checked_all"/></th>
                        <?php
						
				        foreach($indexFields as $k=>$val){
						
		                if ($val['field'] == 'listorder'){?>	
                        <th align="left" width="60"><?php echo $val['name']?></th>
                        <?php
						}else if ($val['field'] == 'status'){?>
                        <th align="center" width="60"><?php echo $val['name']?></th>
                        <?php
						}else{
						?>
                        <th align="left" <?php echo $models['setting']['index']['th'][$val['fieldid']]?>><?php echo $val['name']?></th>
                        <?php
						}
						}
						?>
                        
                        
                        
                        <th class="norightborder" width="200">操作</th>
        </tr>
    </thead>
    </table>
    <div class="template hidden">
    {{each list.data as v i}}
    <table class="view-index-list child-{{v.parentid}}{{each list.depth as vv ii}} child-{{vv}}{{/each}}" style="margin:0; border-bottom:0px;" border="0" cellspacing="0" cellpadding="0" width="100%" id="table-{{v.id}}">
    <tbody class="border-bottom-0">
    <tr>
        <td align="center" width="20"><input type="checkbox" value="{{v.id}}" name="checked_id[]"></td>
		<?php
        
        foreach($indexFields as $k=>$val){
        
			if ($val['field'] == 'listorder'){?>	
			<td align="left" width="60"><input name="listorder[{{v.id}}]" type="text" size="3" value="{{v.listorder}}" class="input-text input-listorder"></td>
			<?php
			}else if ($val['field'] == 'status'){?>
			<td class="td-locked" width="60">
			<div onclick="change_switch(this, '<?php echo $this->createUrl(['/mobo/models-form/status', 'locked'=> '-locked-'])?>&id={{v.id}}&modelid={{list.modelid}}');" id="btn-locked" class="locked-{{v.status}}"></div>
			</td>
			<?php
			 }else if ($val['field'] == 'name'){?>
			
			<td align="left" <?php echo $models['setting']['index']['th'][$val['fieldid']]?>>
			{{each list.depth as vv ii}}{{if ii == 0}}<img  src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/cat_left.gif" />{{else}}<img  src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/cat_child.gif" />{{/if}}{{/each}}
            
            <img class="click_img click_img-{{v.id}}" src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/left_e.gif" onclick="get_child(this, '{{v.id}}');">&nbsp;&nbsp;<input name="name[{{v.id}}]" type="text" style="width:150px" value="{{v.name}}" class="input-text input-<?php echo $val['field']?>">
			</td>
			<?php
			}else{
			?>
			<td align="left" <?php echo $models['setting']['index']['th'][$val['fieldid']]?>>
            <?php if(in_array($val['field'], ['addtime', 'updatetime'])){?>
            {{dateFormat(v.<?php echo $val['field']?>, 'yyyy-MM-dd')}}
            <?php
            }else{
            ?>
            {{v.<?php echo $val['field']?>}}
            <?php
			}
			?>
            </td>
			<?php
			}
        }
        ?>
        <td align="center" class="td-operation" width="200">
        <a href="<?php echo urldecode($this->createUrl(['/mobo/models-form/create', 'parentid'=>'{{v.id}}', 'modelid'=>$modelid]))?>" class="operation-create">添加子分类</a>&nbsp;&nbsp;
        <a href="<?php echo urldecode($this->createUrl(['/mobo/models-form/update', 'id'=>'{{v.id}}', 'modelid'=>$modelid]))?>" title="编辑" class="operation-update">编辑</a>&nbsp;&nbsp;<a href="<?php echo urldecode($this->createUrl(['/mobo/models-form/delete', 'id'=>'{{v.id}}', 'modelid'=>$modelid]))?>" title="删除" class="operation-delete">删除</a></td>
        </tr>
        </tbody>
        
        
        
    
</table>
{{if i == list.count}}
<table class="view-index-list create child-{{v.parentid}}{{each list.depth as vv ii}} child-{{vv}}{{/each}}" style="margin:0; border-bottom:0px;" border="0" cellspacing="0" cellpadding="0" width="100%" id="table-last" onclick="add(this, '{{v.parentid}}');">
<tr>
<td width="20"></td>
<td colspan="<?php echo count($indexFields)-1?>" style="color: #FF6600; cursor:pointer">
{{each list.depth as vv ii}}{{if ii == 0}}{{if i == list.count}}<img src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/cat_last.gif" />{{else}}<img  src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/cat_left.gif" />{{/if}}{{else}}<img  src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/cat_child.gif" />{{/if}}{{/each}}
<img src="<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/act_add.gif" /> 添加{{if v.parentid == 0}}顶级{{else}}{{/if}}分类
</td>
</tr>

</table>
{{/if}}
{{/each}}

</div>
<div class="template loading">
<div style="border:#d3d3d3 1px solid; padding:20px;">正在加载...</div>
</div>
<table class="view-index-list" style="margin:0; " border="0" cellspacing="0" cellpadding="0" width="100%" id="table-last">
<tr>
<td colspan="100">
<input class="btn-button-g confirmSubmit" type="submit" value="添加分类" name="batch_create">
<input class="btn-button-g confirmSubmit ml-10" type="submit" value="批量修改" name="batch_update">
<input class="btn-button-g confirmSubmit ml-10" type="submit" value="批量删除" name="batch_delete">
</td>
</tr>
</table>

<?=$this->csrfToken()?>
</form>

<script>
function add(obj, parentid){
	//alert(parentid)
    //var table = $(obj).parent().parent();

    var table = $('table.child-'+parentid).first().clone();
	table.find('img.click_img').attr('onclick', '').end();
	table.find('input.input-name').val('').attr('name', 'add_ame[]').before('<input name="add_pid[]" type="hidden" value="'+parentid+'">');
	table.find('input.input-listorder').attr('name', 'add_listorder[]');
	table.find('.td-locked').empty();
	table.find('.operation-create,.operation-update').attr('href','javascript:;').css({'color':'#ddd'});
	table.find('.operation-delete').attr('href','javascript:;').click(function(){
		$(this).parentsUntil('table').detach();
	});
	
	$(obj).before(table);
	//$('.template:last #table-'+parentid).after(html);
}
function getdata(parentid, callback){
	var s = new Date().getTime();
	var div = '.template';
    var source = $(div).first().html();
	var render = template.compile(source);	
	var url = '<?php echo $this->createUrl(['mobo/models-form/get-data'])?>';
	$.getJSON(url, {modelid: '<?php echo $modelid?>',parentid: parentid || 0}, function(data){
		//alert(new Date().getTime() - s);
	    var html = render({
			list: data
		});
		if (parentid){
			$('.template:last #table-'+parentid).after(html);
			//$(html).appendTo($(div).last());
		}else{
			$(div).last().empty();
		    $(html).appendTo($(div).last());
		}
		//alert(new Date().getTime() - s);
		callback && callback(data);
	});
	
}

function get_child(obj, id){
	
	var src_e = '<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/left_e.gif';
	var src_d = '<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/left_d.gif';
	var src_loading = '<?php echo dirname(Yii::getAlias('@web'))?>/static/common/images/loading.gif';
	var type = 0;
	if ($(obj).attr('src') == src_d){
		type = 1;
		$('.template:last').find('.child-'+id).remove();
		$('img.click_img-'+id).attr('src', src_e);
		return false;
	}
	if ($('img.click_img-'+id).hasClass('loading')) return false;
	$('img.click_img-'+id).addClass('loading').attr('src', src_loading);
	
	getdata(id, function(d){
		if (d.count == -1){
			artDialog.tips('<font color=red>没有子级分类</font>', 2);
			$('img.click_img-'+id).attr('src', src_e).removeClass('loading');
			return false;
		}
		
		if (type == 0){
			$('img.click_img-'+id).attr('src', src_d);
		}else{
			$('img.click_img-'+id).attr('src', src_e);
		}
		$('img.click_img-'+id).removeClass('loading');
	});
	
}
$(function(){
	getdata();
    /*$('.template:last').find('.click_img').toggle(function() { 
	    var parentid = $(this).attr('parentid');
		alert(parentid);
	    getdata(parentid);
		var tr = $(this).parent().parent();
		//tr.clone().
	},function(){
		
	});*/
});
</script>
<?php $this->FooterView();?>
</body>
</html>